<template>
  <div class="good-list">
    <!-- 面包屑 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品列表</el-breadcrumb-item>
    </el-breadcrumb>

    <!-- 卡片视图区 -->
    <el-card>
      <el-row :gutter="20">
        <el-col :span="8">
          <el-input
            placeholder="请输入内容"
            v-model="goodListInfo.query"
            class="input-with-select"
            clearable
            @clear="chearInp"
          >
            <el-button
              slot="append"
              icon="el-icon-search"
              @click="queryAction"
            ></el-button>
          </el-input>
        </el-col>
        <el-col :span="5">
          <el-button type="primary" @click="goAddPage">添加商品</el-button></el-col
        >
      </el-row>

      <!-- 表格 -->
      <el-table :data="goodList" style="width: 100%" border>
        <el-table-column type="index" label="#"></el-table-column>
        <el-table-column prop="goods_name" label="商品名称" width="600">
        </el-table-column>
        <el-table-column prop="goods_price" label="商品价格"> </el-table-column>
        <el-table-column prop="goods_weight" label="商品重量">
        </el-table-column>
        <el-table-column prop="upd_time" label="创建时间" width="200">
          <template slot-scope="scope">
            {{ scope.row.upd_time | getTime }}
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button
              type="primary"
              icon="el-icon-edit"
              size="mini"
            ></el-button>
            <el-button
              type="danger"
              icon="el-icon-delete"
              size="mini"
              @click="removeGood(scope.row)"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        background
        layout="total, sizes, prev, pager, next, jumper"
        :page-size="goodListInfo.pagesize"
        :total="total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      >
      </el-pagination>
    </el-card>
  </div>
</template>

<script>
import axios from '@/axios/user'
export default {
  name: 'GoodList',
  data() {
    return {
      //商品列表数据
      goodList: [],
      goodListInfo: {
        query: '',
        pagenum: 1,
        pagesize: 10,
      },
      //商品总数
      total: 0,
    }
  },
  methods: {
    //获取表格全部数据
    async getGoodListAll() {
      const { data } = await axios.get('/goods', { params: this.goodListInfo })
      //console.log(data)
      if (data.meta.status !== 200) {
        return this.$message.error(data.meta.msg)
      }
      this.goodList = data.data.goods
      this.total = data.data.total
    },
    //显示条数
    handleSizeChange(val) {
      this.goodListInfo.pagesize = val
      this.getGoodListAll()
    },
    //第几页
    handleCurrentChange(val) {
      this.goodListInfo.pagenum = val
      this.getGoodListAll()
    },
    //查询
    queryAction() {
      this.getGoodListAll()
    },
    chearInp() {
      this.getGoodListAll()
    },
    //删除商品
    async removeGood(row) {
       const result=await this.$confirm('此操作将永久删除该商品, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).catch(err=>err)
        //console.log(result);
        if(result!=='confirm'){
            return this.$message.info('取消删除')
        }
      const { data } = await axios.delete(`/goods/${row.goods_id}`)
      if(data.meta.status!==200){
          return this.$message.error('删除失败')
      }
      this.$message.error('删除成功')
      this.getGoodListAll()
    },
    goAddPage(){
        this.$router.push('/add')
    }
  },
  created() {
    this.getGoodListAll()
  },
}
</script>

<style scoped lang="scss">
</style>